﻿using B.S.Backtrack.ErrorCode;
using B.S.Backtrack.Write.API.Application.Command.Cultures;
using B.S.Backtrack.Write.API.Application.Command.Firms;
using B.S.Backtrack.Write.API.Application.Command.Image;
using B.S.Backtrack.Write.API.Application.Command.Proces;
using B.S.Backtrack.Write.API.Application.Command.Product;
using B.S.Backtrack.Write.API.Application.Command.Sla;
using B.S.Backtrack.Write.API.Application.Command.Supp;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.Backtrack.Write.API.Controllers
{
    /// <summary>
    /// API-写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ManagementController : ControllerBase
    {
        private readonly IMediator mediator;

        public ManagementController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 新增产品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> CreateProduct(CreateProductCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadImages(IFormFile file)
        {
            try
            {
                var res = "wwwroot/Images/" + file.FileName;
                using (FileStream fs = new FileStream(res, FileMode.OpenOrCreate))
                {
                    file.CopyTo(fs);
                }
                return Ok("https://localhost:7160/Images/" + file.FileName);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> UpdateProduct(UpdateProductCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        public Task<ApiResult<int>> DelProduct([FromQuery] DelProductCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 新增图片
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> CreatePicture(CreateImageCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        public Task<ApiResult<int>> DelImage([FromQuery] DelImageCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 新增养殖信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> CreateCulture(CreateCultureCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 新增保健信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> CreateHealth(CreateHealthCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 删除保健信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        public Task<ApiResult<int>> DelHealth([FromQuery] DelHealthCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 修改保健信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> UpdateHealth(UpdateHealthCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 新增养殖企业信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> CreateCulFirm(CreateFirmCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 新增屠宰信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> CreateSlaughter(CreateSlaughterCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 新增屠宰企业信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> CreateSlaFirm(CreateSlaFirmCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 新增深加工信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> CreateProcessing(CreateProcessingCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 新增供应商信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> CreateSupplier(CreateSupplierCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 新增深加工企业信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> CreateProcesFirm(CreateProcesFileCommand request)
        {
            return mediator.Send(request);
        }
    }
}
